﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary.GameObject;
using AimGameLibrary.ScreenManager;

using IrrKlang;

namespace AimGameLibrary
{
    public class AimGameControl
    {
        //ContentManager content;
        //public ContentManager ContenT { get { return content; } }

        //SpriteBatch spriteBatch;
        //public SpriteBatch SpriteBatcH { get { return spriteBatch; } }
        // Use Only Preview mode
        InputState Maininputstate;
        public InputState MaininputstatE { get { return Maininputstate; } }

        List<BaseGameObject> Listbasegameobejct;
        public List<BaseGameObject> ListbasegameobejcT { get { return Listbasegameobejct; } }

        Dictionary<String, BaseGameObject> DictionaryGameObject;
        public Dictionary<String, BaseGameObject> DictionaryGameObjecT { get { return DictionaryGameObject; } }

        List<IDraw> ListDraw;
        public List<IDraw> ListDraW { get { return ListDraw; } }

        List<IUpdateMainTime> ListUpdateTime;
        public List<IUpdateMainTime> ListUpdateTimE { get { return ListUpdateTime; } }

        List<IHandleInputUpdate> ListHandle;
        public List<IHandleInputUpdate> ListHandlE { get { return ListHandle; } }
 
        Camera camera;
        public Camera CamerA { get { return camera; } }

        ISoundEngine engine;
        public ISoundEngine SoundenginE { get { return engine; } }

        public AimGameControl()
        {
            Listbasegameobejct = new List<BaseGameObject>();
            DictionaryGameObject = new Dictionary<string, BaseGameObject>();

            ListDraw = new List<IDraw>();
            ListUpdateTime = new List<IUpdateMainTime>();
            ListHandle = new List<IHandleInputUpdate>();

            Maininputstate = new InputState();
            camera = new Camera("Nodecamera");

            engine = new ISoundEngine();
        }
        
        public void UnLoadContent()
        {
            foreach (String key in DictionaryGameObject.Keys)
                DictionaryGameObject[key].UnLoadContent();

            ListDraw.Clear();
            DictionaryGameObject.Clear();
            Listbasegameobejct.Clear();
            ListUpdateTime.Clear();

            engine.StopAllSounds();
            engine.RemoveAllSoundSources();
        }

        public void UpdateTime(TimeSpan timer)
        {
            /// Update InputState
            Maininputstate.Update();

            #region Update

            List<IUpdateMainTime> tempupdatetime = new List<IUpdateMainTime>();
            foreach (IUpdateMainTime objupdatetime in ListUpdateTime)
                tempupdatetime.Add(objupdatetime);
            foreach (IUpdateMainTime temptime in tempupdatetime)
                temptime.UpdateTime(timer);

            #endregion

            #region Update Handle

            List<IHandleInputUpdate> templisthandle = new List<IHandleInputUpdate>();
            foreach (IHandleInputUpdate objupdatehandle in ListHandle)
                templisthandle.Add(objupdatehandle);
            foreach (IHandleInputUpdate temphandle in templisthandle)
                temphandle.HandleInputUpdateTime(Maininputstate, timer);

            #endregion
        }

        public void UpdateGameTime(GameTime gameTime)
        {
            List<IUpdateMainTime> tempupdategametime = new List<IUpdateMainTime>();
            foreach (IUpdateMainTime objupdatetime in ListUpdateTime)
                tempupdategametime.Add(objupdatetime);
            foreach (IUpdateMainTime tempgametime in tempupdategametime)
                tempgametime.UpdateGameTime(gameTime);
        }

        public void HandleInput(InputState input,GameTime gametime)
        {
            List<IHandleInputUpdate> temphandleinput = new List<IHandleInputUpdate>();
            foreach (IHandleInputUpdate objhandle in ListHandle)
                temphandleinput.Add(objhandle);
            foreach (IHandleInputUpdate temphandle in temphandleinput)
                temphandle.HandleInputUpdateGameTime(input, gametime);
        }

        public void Draw()
        {
            #region Draw Interface concenpt

            List<IDraw> tempdraw = new List<IDraw>();
            foreach (IDraw objdraw in ListDraw)
                tempdraw.Add(objdraw);
            foreach (IDraw draw in tempdraw)
                draw.Draw();

            #endregion
        }
    }
}
